/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import vn.webapp.dao.TeachersDAO;
import vn.webapp.dto.DataPage;
import vn.webapp.model.BusyTeachers;
import vn.webapp.model.Coursing;
import vn.webapp.model.Teachers;
import vn.webapp.model.TeachersToBusy;

@Service("teachersService")
public class TeachersServiceImpl implements TeachersService {

    @Autowired
    private TeachersDAO teachersDAO;

    /**
     *
     * @return
     */
    @Override
    public DataPage<Teachers> list() {
        DataPage<Teachers> dataTeachers = new DataPage<>();
        dataTeachers.setData(teachersDAO.list());
        return dataTeachers;
    }

    @Override
    public Teachers viewDetail(String id) {
        return teachersDAO.viewDetail(id);
    }
    
    @Override
    public Teachers checkExistsEmail(String email, int id){
        return teachersDAO.checkExistsEmail(email, id);
    }

    @Override
    public int updateTheTeacher(String name, String email, int id) {
        return teachersDAO.updateTheTeacher(name, email, id);
    }

    @Override
    public int removeTeacher(int id) {
        return teachersDAO.removeTeacher(id);
    }
    
    @Override
    public int saveATeacher(String name, String email)
    {
        Teachers teachers = new Teachers();
        teachers.setName(name);
        teachers.setEmail(email);
        return teachersDAO.saveATeacher(teachers);
       // return teachersDAO.saveATeacher(name, email);
    }
    
    @Override
    public DataPage<Teachers> filter(int page, int pageSize) {
        DataPage<Teachers> dataTeachers = new DataPage<>();
        dataTeachers.setPageIndex(page);
        dataTeachers.setPageSize(pageSize); 
        dataTeachers.setDataCount(teachersDAO.count());
        dataTeachers.setPageCount(dataTeachers.getDataCount()/pageSize);
        if(dataTeachers.getDataCount()%pageSize > 0){
            dataTeachers.setPageCount(dataTeachers.getPageCount()+1);
        }
        dataTeachers.setData(teachersDAO.filter(page, pageSize));
        return dataTeachers;
    }
    
    @Override
    public List<BusyTeachers> getABusyTeacherByTeacherId(int iTeacherId, int iSessionId, int Day)
    {
        return teachersDAO.getABusyTeacherByTeacherId(iTeacherId, iSessionId, Day);
    }
    
    @Override
    public DataPage<BusyTeachers> listAllBusyTeachers()
    {
        DataPage<BusyTeachers> dataBusyTeachers = new DataPage<>();
        dataBusyTeachers.setData(teachersDAO.listAllBusyTeachers());
        return dataBusyTeachers;
    }
    
    @Override
    public int editBusyTeacherByTeacherId(int iTeacherId, int iSessionId, int iDayId, int iSlot, int iTimeRange){
        
        BusyTeachers busyT = new BusyTeachers();
        busyT.setTeacher_id(iTeacherId);
        busyT.setSession_id(iSessionId);
        busyT.setDay(iDayId);
        busyT.setSlot(iSlot);
        busyT.setTime_range(iTimeRange);
        
        int iRes = teachersDAO.editBusyTeacherByTeacherId(busyT);
           
        return iRes;
    }
    
//    @Override
//    public BusyTeachers checkExistsBusyTeacher(int iTeacherId, int iSessionId, int iDayId)
//    {
//        return teachersDAO.checkExistsBusyTeacher(iTeacherId, iSessionId, iDayId);
//    }
    
    
    @Override
    public int removeBusyTeacher(List<BusyTeachers> oBusyTeachersExists)
    {
        int iRes = 0;
        for(BusyTeachers bst : oBusyTeachersExists)
        {
            iRes += teachersDAO.removeBusyTeacher(bst.getBusyteacher_id());
        }
        return iRes;
    }
    
    @Override
    public DataPage<TeachersToBusy> getAllTeachersToBusy()
    {
        DataPage<TeachersToBusy> teachersToBusy = new DataPage<>();
        teachersToBusy.setData(teachersDAO.getAllTeachersToBusy());
        return teachersToBusy;
    }
    
}
